package scaladuct.validation

import javax.validation.{ValidationException, ConstraintViolation, Validation, ValidatorFactory}
import scaladuct.{Starter, ConfigPageGateway}

/**
 * <p>
 * Created by IntelliJ IDEA.
 * User: zslajchrt
 * Date: Jun 28, 2010
 * Time: 8:33:27 PM
 */
@deprecated("see ValidatingPage")
trait ValidatingGateway[X] extends ConfigPageGateway[X] {
  override def consumeCookie(cookie: AnyRef): Option[Starter[X]] = {
    val validatorFactory: ValidatorFactory = Validation.buildDefaultValidatorFactory();
    val validator: javax.validation.Validator = validatorFactory.getValidator();
    val constraintViolationSet: java.util.Set[ConstraintViolation[AnyRef]] = validator.validate(cookie);

    if (!constraintViolationSet.isEmpty) {
      throw new ConstraintViolationException(constraintViolationSet)
    }

    super.consumeCookie(cookie)
  }

  override def close(): Unit = {
    super.close
  }
}